﻿#include "udpchannelfrm.h"
#include "ui_udpchannelfrm.h"

#include <QDateTime>
#include <QNetworkInterface>

#include <net/udpchannel.h>
QNET_USING_NAMESPACE

UdpChannelFrm::UdpChannelFrm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UdpChannelFrm)
{
    ui->setupUi(this);
    connect(this, &UdpChannelFrm::log_signal, this, &UdpChannelFrm::log_slot);
    m_udpChannel = Q_NULLPTR;

    QList<QHostAddress> iplist = QNetworkInterface::allAddresses();
    ui->comboBoxLocalAddr->addItem("0.0.0.0");  //
    foreach(auto host, iplist)
    {
        if(host.protocol() == QAbstractSocket::IPv6Protocol)
        {
            continue;
        }
        ui->comboBoxLocalAddr->addItem(host.toString());
        if(host.isLoopback() )
        {
            ui->comboBoxLocalAddr->setCurrentText(host.toString());
        }
    }
}

UdpChannelFrm::~UdpChannelFrm()
{
    delete ui;
}

void UdpChannelFrm::onProc(const QByteArray &arry)
{
    QString msg = QString(QStringLiteral("接收到 来自%1:%2 发送的%3字节 数据:%4"))
            .arg(this->host().toString())
            .arg(int(this->port()))
            .arg(arry.size())
            .arg(QString::fromUtf8(arry));
    emit log_signal(msg);
}

void UdpChannelFrm::setStopEnbale(bool enable)
{
    ui->btnSendData->setEnabled(enable);
    ui->btnStart->setEnabled(!enable);
    ui->btnStop->setEnabled(enable);
}

void UdpChannelFrm::on_btnClearLog_clicked()
{
    ui->textBrowserLog->clear();
}

void UdpChannelFrm::on_btnStart_clicked()
{
    QString localAddr = ui->comboBoxLocalAddr->currentText();
    QHostAddress srcAddr(localAddr);
    quint16 srcPort = quint16(ui->spinLocalPort->value());
    QString msg = QString(tr("绑定本机地址%1:%2"))
            .arg(srcAddr.toString())
            .arg(srcPort);
    if( m_udpChannel )
    {
         m_udpChannel->deleteLater();
    }
    m_udpChannel = new UdpChannel;
    if(!m_udpChannel->bind(srcAddr, srcPort))
    {
        msg.append(tr("失败"));
    }else {
        m_udpChannel->setReading(this);
        msg.append(tr("成功"));
        setStopEnbale(true);
    }
    emit log_signal(msg);
    return;
}

void UdpChannelFrm::log_slot(QString msg)
{
    QString showMsg = msg;
    showMsg.insert(0, QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz "));
    ui->textBrowserLog->append(showMsg);
}

void UdpChannelFrm::on_btnSendData_clicked()
{
    if(!m_udpChannel)
    {
        return ;
    }
    QString sendText = ui->textEditSend->toPlainText();
    QString addr = ui->eidtDstAddr->text();
    QHostAddress remoteAddr(addr);
    quint16 remotePort = quint16(ui->spinDstPort->value());
    int writeSize = m_udpChannel->writeTo(QByteArray(sendText.toUtf8()), remoteAddr, remotePort);
    ui->labSendSize->setNum(writeSize);

    QString msg = QString(tr("向%1:%2发送数据:%3字节"))
            .arg(remoteAddr.toString())
            .arg(remotePort)
            .arg(writeSize);
    emit log_signal(msg);
}

void UdpChannelFrm::on_btnClearSend_clicked()
{
    ui->textEditSend->clear();
}

void UdpChannelFrm::on_btnStop_clicked()
{
    if( m_udpChannel )
    {
         m_udpChannel->deleteLater();
         m_udpChannel = Q_NULLPTR;
    }
    QString msg(tr("关闭"));
    emit log_signal(msg);
    setStopEnbale(false);
}
